import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;

import javax.swing.*;
public class guanliyuanjiemian extends JFrame{

    JButton zhucechangdi;
    JButton changdiqingkuang;
    JButton shouru;
    JButton weijiaofei;
    JButton fanhuizhuye;

    guanliyuanjiemianjianshiqi guanliyuanjiemianjianshiqi1;
    
    public guanliyuanjiemian()
    {
        chushihua();
        //setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    void chushihua()//初始化
    {
        setBounds(400,400,400,400);
        setTitle("管理员");
        setLayout(null);

        guanliyuanjiemianjianshiqi1=new guanliyuanjiemianjianshiqi(this);
        
        zhucechangdi=new JButton("注册场地");
        changdiqingkuang=new JButton("场地情况");
        shouru=new JButton("收入");
        weijiaofei=new JButton("未缴费人");
        fanhuizhuye=new JButton("退出");

        zhucechangdi.setBounds(150,50,100,30);
        changdiqingkuang.setBounds(150,100,100,30);
        shouru.setBounds(150,150,100,30);
        weijiaofei.setBounds(150,200,100,30);
        fanhuizhuye.setBounds(150,250,100,30);

        add(zhucechangdi);
        add(changdiqingkuang);
        add(shouru);
        add(weijiaofei);
        add(fanhuizhuye);

        zhucechangdi.addActionListener(guanliyuanjiemianjianshiqi1);
        changdiqingkuang.addActionListener(guanliyuanjiemianjianshiqi1);
        shouru.addActionListener(guanliyuanjiemianjianshiqi1);
        weijiaofei.addActionListener(guanliyuanjiemianjianshiqi1);
        fanhuizhuye.addActionListener(guanliyuanjiemianjianshiqi1);
    }

    void guanlichangdi()//打开管理场地窗口
    {
        guanlichangdichuangkou guanlichangdichuangkou1=new guanlichangdichuangkou(this);
    }
    void guanlichangdi(int yemian)
    {
        guanlichangdichuangkou guanlichangdichuangkou1=new guanlichangdichuangkou(this,yemian);
    }
    void zhucechangdichuangkou(){   //注册场地个数窗口
        int changdigeshu=0;
        String str = JOptionPane.showInputDialog(null, "输入注册场地个数", "注册", JOptionPane.PLAIN_MESSAGE);
        if (str != null) {
            try {
                changdigeshu = Integer.parseInt(str);
            } catch (Exception exp) {

            }
        }
        for(int i=0;i<changdigeshu;i++)
        {
            try {
                xieru.zhuijia("0","shuju\\zuyongshijian.txt");
                xieru.zhuijia("0","shuju\\chuzuqingkuang.txt");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(changdigeshu<50&&changdigeshu>0){
            JOptionPane.showMessageDialog(null,"注册成功","信息",1);

        }
        else{
            changdigeshu = 0;
            JOptionPane.showMessageDialog(null,"注册失败","信息",JOptionPane.WARNING_MESSAGE);
        }
    }
    void chaxunshouruchuangkou()
    {
        shouruchuangkou shouruchuangkou1=new shouruchuangkou(this,0);
    }
    void chaxunshouruchuangkou(int yemian)
    {
        shouruchuangkou shouruchuangkou1=new shouruchuangkou(this,yemian);
    }
    void weijiaofei()
    {
        weijiaofeirenmingdan weijiaofeirenmingdan1=new weijiaofeirenmingdan(this, 0);
    }
    void weijiaofei(int yemian)
    {
        weijiaofeirenmingdan weijiaofeirenmingdan1=new weijiaofeirenmingdan(this, yemian);
    }
    void fanhuizhuye()//退出
    {
        main.fanhuizhuye();
    }

}
class guanlichangdichuangkou extends JFrame//管理场地窗口
{
    int anniushu=10;
    JButton changdi[];
    JButton shangyiye;
    JButton xiayiye;
    JTextField changdixinxi[];
    guanlichangdichuangkoujianshiqi guanlichangdichuangkoujianshiqi1;
    guanliyuanjiemian guanliyuanjiemian1;
    int yemian=0;
    guanlichangdichuangkou(guanliyuanjiemian guanliyuanjiemian1)
    {
        kaiqicichuangkou(guanliyuanjiemian1,0);
    }
    guanlichangdichuangkou(guanliyuanjiemian guanliyuanjiemian1,int yemian)
    {
        kaiqicichuangkou(guanliyuanjiemian1,yemian);
    }
    private void kaiqicichuangkou(guanliyuanjiemian guanliyuanjiemian1, int yemian) {
        this.setTitle("管理场地");
        
        this.setVisible(true);
        
        this.setLayout(null);
        this.setBounds(100,150 , 600, 600);
        guanlichangdichuangkoujianshiqi1=new guanlichangdichuangkoujianshiqi(this);
        this.guanliyuanjiemian1=guanliyuanjiemian1;
        this.yemian=yemian;

        shuaxinshuju(yemian);
    }
    
    void shuaxinshuju()
    {
        shuaxinshuju(yemian);
    }
    void shuaxinshuju(int yemian)
    {
        this.yemian=yemian;
        changdi=new JButton[11];
        changdixinxi=new JTextField[11];
        for(int i=1;i<=10;i++)
        {
            changdi[i]=new JButton(Integer.toString(i+10*yemian));
            this.add(changdi[i]);
            changdi[i].setBounds(50,20+(i-1)*50,100,40);
            changdi[i].addActionListener(guanlichangdichuangkoujianshiqi1);

            String xinxi="0";
            try {
                xinxi=duqu.duqu(i+10*yemian,"shuju\\chuzuqingkuang.txt");
            } catch (IOException e) {
                xinxi="程序读取异常";
            }
            if(xinxi.equals("0"))
            {
                changdixinxi[i]=new JTextField("无人租用，点击按钮停用");
                changdi[i].setEnabled(true);
            }
            else if(xinxi.equals("-1"))
            {
                changdixinxi[i]=new JTextField("当前停用，点击取消停用");
                changdi[i].setEnabled(true);
            }
            else if(xinxi.equals("没有找到数据"))
            { 
                changdi[i].setEnabled(false);
                changdi[i].setVisible(false);
                anniushu=i-1;
                break;
            }
            else 
            {
                changdixinxi[i]=new JTextField("已被租用，租用者"+xinxi);
                changdi[i].setEnabled(false);
            }
            this.add(changdixinxi[i]);
            changdixinxi[i].setBounds(200,20+(i-1)*50,300,40);
            changdixinxi[i].setEditable(false);
        }
        shangyiye=new JButton("上一页");
        this.add(shangyiye);
        shangyiye.setBounds(50,520, 80, 30);
        shangyiye.setVisible(true);
        shangyiye.addActionListener(guanlichangdichuangkoujianshiqi1);
        if(yemian==0)
        {
            shangyiye.setEnabled(false);
        }
        else
        {
            shangyiye.setEnabled(true);
        }
        xiayiye=new JButton("下一页");
        this.add(xiayiye);
        xiayiye.setBounds(300,520, 80, 30);
        xiayiye.setVisible(true);
        xiayiye.addActionListener(guanlichangdichuangkoujianshiqi1);
        if(anniushu!=10)
        {
            xiayiye.setEnabled(false);
        }
        else 
        {
            xiayiye.setEnabled(true);
        }
    }

    void qiyonghuotingyongchangdi(int changdi)//启用或禁用场地
    {
        try 
        {
            if(duqu.duqu(changdi,"shuju\\chuzuqingkuang.txt").equals("-1"))//场地已禁用，点击按钮启用
            {
                qiyongchangdi(changdi);
                JOptionPane.showMessageDialog(this, "场地"+changdi+"启用成功");
            }
            else 
            {
                jinyongchangdi(changdi);
                JOptionPane.showMessageDialog(this, "场地"+changdi+"禁用成功");
            }
        }
        catch (IOException e) 
        {
            
        }
        int yemian=this.yemian;
        this.guanliyuanjiemian1.guanlichangdi(yemian);
        this.setVisible(false);
    }
    
    void jinyongchangdi(int changdi)//禁用场地
    {
        try {
            xieru.xie(changdi, "-1","shuju\\chuzuqingkuang.txt");
        } catch (IOException e) {
            return;
        }
    }
    void qiyongchangdi(int changdi)//启用场地
    {
        try {
            xieru.xie(changdi, "0","shuju\\chuzuqingkuang.txt");
        } catch (IOException e) {
            return;
        }
    }

    /*void guanlihuoguihuanchangdi(int zhanghao,int changdi)
    {
        try 
        {
            if(duqu.duqu(changdi).equals(String.valueOf(yonghujiemian1.zhanghao)))//用户已租用，点击按钮归还
            {
                guihuanchangdi(zhanghao, changdi);
                JOptionPane.showMessageDialog(this, "场地"+changdi+"归还成功");
            }
            else 
            {
                guanlichangdi(zhanghao, changdi);
                JOptionPane.showMessageDialog(this, "场地"+changdi+"租用成功");
            }
        }
        catch (IOException e) 
        {
            
        }
        this.setVisible(false);
    }

    void guanlichangdi(int zhanghao,int changdi)
    {
        try {
            xieru.xie(changdi, Integer.toString(zhanghao),"shuju\\chuzuqingkuang.txt");
        } catch (IOException e) {
            return;
        }
        try {
            long shijianchuomiao=Math.floorDiv(System.currentTimeMillis(), 1000);
            xieru.xie(changdi,String.valueOf(shijianchuomiao),"shuju\\guanlishijian.txt");
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    void guihuanchangdi(int zhanghao,int changdi)
    {
        try {
            xieru.xie(changdi, "0","shuju\\chuzuqingkuang.txt");
        } catch (IOException e) {
            return;
        }
        try {
            long shijianchuomiao=Math.floorDiv(System.currentTimeMillis(), 1000);
            xieru.xie(changdi,"0","shuju\\guanlishijian.txt");
        } catch (Exception e) {
            // TODO: handle exception
        }
    }*/
    
}

class shouruchuangkou extends JFrame
{
    int yemian=0;
    guanliyuanjiemian guanliyuanjiemian1;
    JButton shangyiye;
    JButton xiayiye;

    shouruchuangkoujianshiqi shouruchuangkoujianshiqi1;
    shouruchuangkou(guanliyuanjiemian guanliyuanjiemian1)
    {
        kaiqicichuangkou(guanliyuanjiemian1, 0);
    }
    shouruchuangkou(guanliyuanjiemian guanliyuanjiemian1,int yemian)
    {
        kaiqicichuangkou(guanliyuanjiemian1, yemian);
    }
    private void kaiqicichuangkou(guanliyuanjiemian guanliyuanjiemian1,int yemian)
    {
        this.yemian=yemian;
        this.guanliyuanjiemian1=guanliyuanjiemian1;
        setTitle("查询收入窗口");
        setVisible(true);
        setBounds(400,400,400,400);
        setLayout(null);
        shouruchuangkoujianshiqi1=new shouruchuangkoujianshiqi(this);

        if(yemian!=0)
        {
            shangyiye=new JButton("上一页");
            shangyiye.addActionListener(shouruchuangkoujianshiqi1);
            shangyiye.setVisible(true);
            shangyiye.setBounds(0, 300, 80, 30);
            add(shangyiye);
        }
        xiayiye=new JButton("下一页");
        xiayiye.addActionListener(shouruchuangkoujianshiqi1);
        xiayiye.setVisible(true);
        xiayiye.setBounds(300, 300, 80, 30);
        add(xiayiye);

        JTextField xiangmuxinxi[];
        JTextField jiaofeixinxi[][];
        xiangmuxinxi = new JTextField[3];
        jiaofeixinxi = new JTextField[10][3];
        String[]sti = {"用户","场地","金额"};
        for(int i = 0; i<3; i++){
            xiangmuxinxi[i]=new JTextField(sti[i]);
            this.add(xiangmuxinxi[i]);
            xiangmuxinxi[i].setBounds(100+(i*100),0,70,40);
            xiangmuxinxi[i].setEditable(false);

        }


        for(int i=1;i<=5;i++)    //显示收入信息
        {
            String xinxi="0";//读取
            try {
                xinxi=duqu.duqu(i+yemian*5,"shuju\\shouru.txt");
            } catch (IOException e) {
                xinxi="程序读取异常";
            }
            if(xinxi.equals("没有找到数据"))
            {
                xiayiye.setVisible(false);
                break;
            }
            int b[] = fanhuishuzu.threeint(xinxi);

            jiaofeixinxi[i][0]=new JTextField(""+b[0]);
            this.add(jiaofeixinxi[i][0]);
            jiaofeixinxi[i][0].setBounds(100,60+(i-1)*50,70,40);
            jiaofeixinxi[i][0].setEditable(false);

            
            jiaofeixinxi[i][1]=new JTextField(""+b[1]);
            this.add(jiaofeixinxi[i][1]);
            jiaofeixinxi[i][1].setBounds(200,60+(i-1)*50,70,40); 
            jiaofeixinxi[i][1].setEditable(false);


            jiaofeixinxi[i][2]=new JTextField(""+b[2]);
            this.add(jiaofeixinxi[i][2]);
            jiaofeixinxi[i][2].setBounds(300,60+(i-1)*50,70,40);
            jiaofeixinxi[i][2].setEditable(false);
        }






    }
}

class weijiaofeirenmingdan extends JFrame   //未缴费用人信息窗口
{
    int yemian=0;
    guanliyuanjiemian guanliyuanjiemian1;
    JButton shangyiye;
    JButton xiayiye;

    weijiaofeirenmingdanjianshiqi weijiaofeirenmingdanjianshiqi1;
    weijiaofeirenmingdan(guanliyuanjiemian guanliyuanjiemian1)
    {
        kaiqicichuangkou(guanliyuanjiemian1, 0);
    }
    weijiaofeirenmingdan(guanliyuanjiemian guanliyuanjiemian1,int yemian)
    {
        kaiqicichuangkou(guanliyuanjiemian1, yemian);
    }
    private void kaiqicichuangkou(guanliyuanjiemian guanliyuanjiemian1,int yemian)
    {
        this.yemian=yemian;
        this.guanliyuanjiemian1=guanliyuanjiemian1;
        setTitle("未缴费用人窗口");
        setVisible(true);
        setBounds(400,400,400,400);
        setLayout(null);
        weijiaofeirenmingdanjianshiqi1=new weijiaofeirenmingdanjianshiqi(this);

        if(yemian!=0)
        {
            shangyiye=new JButton("上一页");
            shangyiye.addActionListener(weijiaofeirenmingdanjianshiqi1);
            shangyiye.setVisible(true);
            shangyiye.setBounds(0, 300, 80, 30);
            add(shangyiye);
        }
        xiayiye=new JButton("下一页");
        xiayiye.addActionListener(weijiaofeirenmingdanjianshiqi1);
        xiayiye.setVisible(true);
        xiayiye.setBounds(300, 300, 80, 30);
        add(xiayiye);

        JTextField xiangmuxinxi[];
        JTextField weijiaofeiyongxinxi[][];
        xiangmuxinxi = new JTextField[3];
        weijiaofeiyongxinxi = new JTextField[10][3];
        String[]sti = {"用户","场地","租用时间"};
        for(int i = 0; i<3; i++){
            xiangmuxinxi[i]=new JTextField(sti[i]);
            this.add(xiangmuxinxi[i]);
            xiangmuxinxi[i].setBounds(100+(i*100),0,70,40);
            xiangmuxinxi[i].setEditable(false);

        }
        for(int i=1;i<=5;i++)    //显示未缴费人信息
        {
            String xinxi="0";//读取
            try {
                xinxi=duqu.duqu(i+yemian*5,"shuju\\weijiaomingdan.txt");
            } catch (IOException e) {
                xinxi="程序读取异常";
            }
            if(xinxi.equals("没有找到数据"))
            {
                xiayiye.setVisible(false);
                break;
            }
            int b[] = fanhuishuzu.threeint(xinxi);

            weijiaofeiyongxinxi[i][0]=new JTextField(""+b[0]);
            this.add(weijiaofeiyongxinxi[i][0]);
            weijiaofeiyongxinxi[i][0].setBounds(100,60+(i-1)*50,70,40);
            weijiaofeiyongxinxi[i][0].setEditable(false);

            
            weijiaofeiyongxinxi[i][1]=new JTextField(""+b[1]);
            this.add(weijiaofeiyongxinxi[i][1]);
            weijiaofeiyongxinxi[i][1].setBounds(200,60+(i-1)*50,70,40); 
            weijiaofeiyongxinxi[i][1].setEditable(false);


            weijiaofeiyongxinxi[i][2]=new JTextField(""+b[2]);
            this.add(weijiaofeiyongxinxi[i][2]);
            weijiaofeiyongxinxi[i][2].setBounds(300,60+(i-1)*50,70,40);
            weijiaofeiyongxinxi[i][2].setEditable(false);
        }

    }
}
class weijiaofeirenmingdanjianshiqi implements ActionListener   //未缴费人监视器
{
    weijiaofeirenmingdan weijiaofeirenmingdan1;
    weijiaofeirenmingdanjianshiqi(weijiaofeirenmingdan weijiaofeirenmingdan1)
    {
        this.weijiaofeirenmingdan1=weijiaofeirenmingdan1;
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==weijiaofeirenmingdan1.shangyiye)
        {
            int yemian=weijiaofeirenmingdan1.yemian;
            yemian--;
            weijiaofeirenmingdan1.guanliyuanjiemian1.weijiaofei(yemian);
            weijiaofeirenmingdan1.setVisible(false);
        }
        else if(e.getSource()==weijiaofeirenmingdan1.xiayiye)
        {
            int yemian=weijiaofeirenmingdan1.yemian;
            yemian++;
            weijiaofeirenmingdan1.guanliyuanjiemian1.weijiaofei(yemian);
            weijiaofeirenmingdan1.setVisible(false);
        }
    }
    
}
class shouruchuangkoujianshiqi implements ActionListener
{
    shouruchuangkou shouruchuangkou1;
    shouruchuangkoujianshiqi(shouruchuangkou shouruchuangkou1)
    {
        this.shouruchuangkou1=shouruchuangkou1;
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==shouruchuangkou1.shangyiye)
        {
            int yemian=shouruchuangkou1.yemian;
            yemian--;
            shouruchuangkou1.guanliyuanjiemian1.chaxunshouruchuangkou(yemian);
            shouruchuangkou1.setVisible(false);
        }
        else if(e.getSource()==shouruchuangkou1.xiayiye)
        {
            int yemian=shouruchuangkou1.yemian;
            yemian++;
            shouruchuangkou1.guanliyuanjiemian1.chaxunshouruchuangkou(yemian);
            shouruchuangkou1.setVisible(false);
        }
    }
    
}
class guanliyuanjiemianjianshiqi implements ActionListener//管理员界面的监视器 
{
    guanliyuanjiemian guanliyuanjiemian1;
    
    guanliyuanjiemianjianshiqi(guanliyuanjiemian guanliyuanjiemian1)
    {
        this.guanliyuanjiemian1=guanliyuanjiemian1;
    }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==guanliyuanjiemian1.fanhuizhuye)
        {
            guanliyuanjiemian1.fanhuizhuye();
        }
        else if(e.getSource()==guanliyuanjiemian1.changdiqingkuang)
        {
            guanliyuanjiemian1.guanlichangdi();
        }
        else if (e.getSource() == guanliyuanjiemian1.zhucechangdi) {
            guanliyuanjiemian1.zhucechangdichuangkou();
        }
        else if(e.getSource()==guanliyuanjiemian1.shouru)
        {
            guanliyuanjiemian1.chaxunshouruchuangkou();
        }
        else if(e.getSource()==guanliyuanjiemian1.weijiaofei)
        {
            guanliyuanjiemian1.weijiaofei();
        }
    }
}

class guanlichangdichuangkoujianshiqi implements ActionListener//管理场地窗口的监视器
{
    guanlichangdichuangkou guanlichangdichuangkou1;
    guanlichangdichuangkoujianshiqi(guanlichangdichuangkou guanlichangdichuangkou1)
    {
        this.guanlichangdichuangkou1=guanlichangdichuangkou1;
    }
    public void actionPerformed(ActionEvent e) 
    {
        int changdi=0;
        if(e.getSource()==guanlichangdichuangkou1.changdi[1])
        {
            changdi=1;
        }
        else if(e.getSource()==guanlichangdichuangkou1.changdi[2])
        {
            changdi=2;
        }
        else if(e.getSource()==guanlichangdichuangkou1.changdi[3])
        {
            changdi=3;
        }
        else if(e.getSource()==guanlichangdichuangkou1.changdi[4])
        {
            changdi=4;
        }
        else if(e.getSource()==guanlichangdichuangkou1.changdi[5])
        {
            changdi=5;
        }
        else if(e.getSource()==guanlichangdichuangkou1.changdi[6])
        {
            changdi=6;
        }
        else if(e.getSource()==guanlichangdichuangkou1.changdi[7])
        {
            changdi=7;
        }
        else if(e.getSource()==guanlichangdichuangkou1.changdi[8])
        {
            changdi=8;
        }
        else if(e.getSource()==guanlichangdichuangkou1.changdi[9])
        {
            changdi=9;
        }
        else if(e.getSource()==guanlichangdichuangkou1.changdi[10])
        {
            changdi=10;
        }else if(e.getSource()==guanlichangdichuangkou1.shangyiye)
        {
            int yemian=guanlichangdichuangkou1.yemian;
            yemian--;
            guanlichangdichuangkou1.guanliyuanjiemian1.guanlichangdi(yemian);
            guanlichangdichuangkou1.setVisible(false);
            return;
        }
        else if(e.getSource()==guanlichangdichuangkou1.xiayiye)
        {
            int yemian=guanlichangdichuangkou1.yemian;
            yemian++;
            guanlichangdichuangkou1.guanliyuanjiemian1.guanlichangdi(yemian);
            guanlichangdichuangkou1.setVisible(false);
            return;
        }
        
        changdi+=guanlichangdichuangkou1.yemian*10;
        guanlichangdichuangkou1.qiyonghuotingyongchangdi(changdi);
        guanlichangdichuangkou1.shuaxinshuju();
    }
}